<template>
  <div>
    <div style="margin-bottom: 20px;">
      <el-input v-model="searchQuery" placeholder="请输入专利名称进行查询" style="width: 200px; margin-right: 10px;" />
      <el-button type="primary" @click="handleSearch">查询</el-button>
      <el-button @click="handleReset">重置</el-button>
    </div>
    <el-button type="primary" @click="handleAdd"> 添加专利</el-button>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="patentName" label="专利名称" width="180" />
      <el-table-column prop="patentProperty" label="专利属性" width="180" />
      <el-table-column prop="patentCategory" label="专利类别" width="180" />
      <el-table-column prop="authorizationStatus" label="授权状态" width="180" />
      <el-table-column prop="applicationNumber" label="申请号" width="180" />
      <el-table-column prop="publicationNumber" label="公开号" width="180" />
      <el-table-column prop="authorizationNumber" label="授权号" width="180" />
      <el-table-column prop="inventor" label="发明人" width="180" />
      <el-table-column prop="patentHolder" label="专利权人" width="180" />
      <el-table-column prop="applicationDate" label="申请日期" width="180" />
      <el-table-column prop="authorizationDate" label="授权日期" width="180" />
      <el-table-column prop="patentCertificate" label="专利证书" width="180" />
      <el-table-column prop="reportDate" label="填报日期" width="180" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button round type="warning" @click="handleUpdate(scope.row)">更新</el-button>
          <el-popconfirm title="你确定要删除吗?" @confirm="handleDelete(scope.row.id)" confirm-button-text="确定" cancel-button-text="取消">
            <template #reference>
              <el-button round type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加对话框 -->
    <el-dialog v-model="dialogVisible" title="添加专利">
      <el-form ref="addFormRef" :model="addForm" :rules="rules" label-width="100px" class="ruleForm" status-icon>
        <el-form-item label="专利名称" prop="patentName">
          <el-input v-model="addForm.patentName"/>
        </el-form-item>
        <el-form-item label="专利属性" prop="patentProperty">
          <el-input v-model="addForm.patentProperty"/>
        </el-form-item>
        <el-form-item label="专利类别" prop="patentCategory">
          <el-input v-model="addForm.patentCategory"/>
        </el-form-item>
        <el-form-item label="授权状态" prop="authorizationStatus">
          <el-input v-model="addForm.authorizationStatus"/>
        </el-form-item>
        <el-form-item label="申请号" prop="applicationNumber">
          <el-input v-model="addForm.applicationNumber"/>
        </el-form-item>
        <el-form-item label="公开号" prop="publicationNumber">
          <el-input v-model="addForm.publicationNumber"/>
        </el-form-item>
        <el-form-item label="授权号" prop="authorizationNumber">
          <el-input v-model="addForm.authorizationNumber"/>
        </el-form-item>
        <el-form-item label="发明人" prop="inventor">
          <el-input v-model="addForm.inventor"/>
        </el-form-item>
        <el-form-item label="专利权人" prop="patentHolder">
          <el-input v-model="addForm.patentHolder"/>
        </el-form-item>
        <el-form-item label="申请日期" prop="applicationDate">
          <el-date-picker v-model="addForm.applicationDate" type="date" placeholder="选择日期"/>
        </el-form-item>
        <el-form-item label="授权日期" prop="authorizationDate">
          <el-date-picker v-model="addForm.authorizationDate" type="date" placeholder="选择日期"/>
        </el-form-item>
        <el-form-item label="专利证书" prop="patentCertificate">
          <el-input v-model="addForm.patentCertificate"/>
        </el-form-item>
        <el-form-item label="填报日期" prop="reportDate">
          <el-date-picker v-model="addForm.reportDate" type="date" placeholder="选择日期"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible=false">取消</el-button>
          <el-button type="primary" @click="handleAddConfirm">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 更新对话框 -->
    <el-dialog v-model="dialogUpdateVisible" title="更新专利">
      <el-form ref="updateFormRef" :model="updateForm" :rules="rules" label-width="100px" class="ruleForm" status-icon>
        <el-form-item label="专利名称" prop="patentName">
          <el-input v-model="updateForm.patentName"/>
        </el-form-item>
        <el-form-item label="专利属性" prop="patentProperty">
          <el-input v-model="updateForm.patentProperty"/>
        </el-form-item>
        <el-form-item label="专利类别" prop="patentCategory">
          <el-input v-model="updateForm.patentCategory"/>
        </el-form-item>
        <el-form-item label="授权状态" prop="authorizationStatus">
          <el-input v-model="updateForm.authorizationStatus"/>
        </el-form-item>
        <el-form-item label="申请号" prop="applicationNumber">
          <el-input v-model="updateForm.applicationNumber"/>
        </el-form-item>
        <el-form-item label="公开号" prop="publicationNumber">
          <el-input v-model="updateForm.publicationNumber"/>
        </el-form-item>
        <el-form-item label="授权号" prop="authorizationNumber">
          <el-input v-model="updateForm.authorizationNumber"/>
        </el-form-item>
        <el-form-item label="发明人" prop="inventor">
          <el-input v-model="updateForm.inventor"/>
        </el-form-item>
        <el-form-item label="专利权人" prop="patentHolder">
          <el-input v-model="updateForm.patentHolder"/>
        </el-form-item>
        <el-form-item label="申请日期" prop="applicationDate">
          <el-date-picker v-model="updateForm.applicationDate" type="date" placeholder="选择日期"/>
        </el-form-item>
        <el-form-item label="授权日期" prop="authorizationDate">
          <el-date-picker v-model="updateForm.authorizationDate" type="date" placeholder="选择日期"/>
        </el-form-item>
        <el-form-item label="专利证书" prop="patentCertificate">
          <el-input v-model="updateForm.patentCertificate"/>
        </el-form-item>
        <el-form-item label="填报日期" prop="reportDate">
          <el-date-picker v-model="updateForm.reportDate" type="date" placeholder="选择日期"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogUpdateVisible=false">取消</el-button>
          <el-button type="primary" @click="handleUpdateConfirm">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>


<script setup>
import { onMounted, reactive, ref } from "vue";
import axios from "axios";

const tableData = ref([]);
const searchQuery = ref(""); // 查询输入框的绑定变量

const getList = async (query = "") => {
  try {
    let res;
    if (query) {
      res = await axios.get(`http://localhost:8000/adminapi/patents?patentName=${query}`);
    } else {
      res = await axios.get('http://localhost:8000/adminapi/patents');
    }
    tableData.value = res.data;
  } catch (error) {
    console.error('Error fetching data:', error);
  }
}

// 显示添加对话框
const handleAdd = () => {
  dialogVisible.value = true;
}
const dialogVisible = ref(false);
const addFormRef = ref();
const addForm = reactive({
  patentName: "",
  patentProperty: "",
  patentCategory: "",
  authorizationStatus: "",
  applicationNumber: "",
  publicationNumber: "",
  authorizationNumber: "",
  inventor: "",
  patentHolder: "",
  applicationDate: "",
  authorizationDate: "",
  patentCertificate: "",
  reportDate: ""
});
const rules = reactive({
  patentName: [
    { required: true, message: '请输入专利名称', trigger: 'blur' },
  ],
  // 其他验证规则
})
const handleAddConfirm = () => {
  addFormRef.value.validate(async (valid) => {
    if (valid) {
      dialogVisible.value = false;
      await axios.post('http://localhost:8000/adminapi/patents', addForm);
      await getList();
      // 清空表单
      Object.keys(addForm).forEach(key => addForm[key] = "");
    }
  })
}

onMounted(() => {
  getList();
})

// 更新
const dialogUpdateVisible = ref(false);
const updateFormRef = ref();
const currentItem = ref();
const updateForm = reactive({
  patentName: "",
  patentProperty: "",
  patentCategory: "",
  authorizationStatus: "",
  applicationNumber: "",
  publicationNumber: "",
  authorizationNumber: "",
  inventor: "",
  patentHolder: "",
  applicationDate: "",
  authorizationDate: "",
  patentCertificate: "",
  reportDate: ""
});

const handleUpdate = (item) => {
  dialogUpdateVisible.value = true;
  Object.assign(updateForm, item);
  currentItem.value = item;
}
const handleUpdateConfirm = async () => {
  try {
    dialogUpdateVisible.value = false;
    if (currentItem.value.id !== undefined) {
      await axios.put(`http://localhost:8000/adminapi/patents/${currentItem.value.id}`, updateForm);
      await getList();
    } else {
      console.error('Patent ID is undefined');
    }
  } catch (error) {
    console.error('Error updating patent:', error.response.data);
  }
};

// 删除
const handleDelete = async (id) => {
  try {
    if (id !== undefined) {
      await axios.delete(`http://localhost:8000/adminapi/patents/${id}`);
      await getList();
    } else {
      console.error('Patent ID is undefined');
    }
  } catch (error) {
    console.error('Error deleting patent:', error.response.data);
  }
};

// 查询
const handleSearch = () => {
  getList(searchQuery.value);
}

// 重置
const handleReset = () => {
  searchQuery.value = "";
  getList();
}
</script>


